﻿function isPhone () {
  let mobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
  return mobile != null;
}
$(document).ready(function () {
  var s = document.location;
  $("#nav a").each(function () {
    if (this.href == s.toString().split("#")[0]) {
      $(this).addClass("on");
      return false;
    }
  })
})
//  导航
$.fn.menumaker = function (options) {

  var cssmenu = $(this), settings = $.extend({
    title: "Menu",
    format: "dropdown",
    sticky: false
  }, options);

  return this.each(function () {
    cssmenu.prepend('<div id="menu-button">' + settings.title + '</div>');
    $(this).find("#menu-button").on('click', function () {
      $(this).toggleClass('menu-opened');
      var mainmenu = $(this).next('ul');
      if (mainmenu.hasClass('open')) {
        mainmenu.hide().removeClass('open');
      }
      else {
        mainmenu.show().addClass('open');
        if (settings.format === "dropdown") {
          mainmenu.find('ul').show();
        }
      }
    });

    cssmenu.find('li ul').parent().addClass('has-sub');

    multiTg = function () {
      for (let i = 0; i < cssmenu.find('li').length; i++) {
        if(cssmenu.find('li')[i].className == "navbar-item has-sub"){
          cssmenu.find(".navbar-item.has-sub").prepend('<span class="submenu-button"></span>');
        }
        
      }
      cssmenu.find('.submenu-button').on('click', function () {
        $(this).toggleClass('submenu-opened');
        if ($(this).siblings('ul').hasClass('open')) {
          $(this).siblings('ul').removeClass('open').hide();
        }
        else {
          $(this).siblings('ul').addClass('open').show();
        }
      });
    };

    if (settings.format === 'multitoggle') multiTg();
    else cssmenu.addClass('dropdown');

    if (settings.sticky === true) cssmenu.css('position', 'fixed');

    resizeFix = function () {
      if (!isPhone() && $(window).width() > 960) {
        cssmenu.find('ul').show();
      }
      if (isPhone() && $(window).width() <= 960) {
        cssmenu.find('ul').hide().removeClass('open');
      }
    };
    resizeFix();
    return $(window).on('resize', resizeFix);

  });
};
$(document).ready(function () {

  $(document).ready(function () {
    $("#nav").menumaker({
      title: "",
      format: "multitoggle"
    });

    $("#nav").prepend("<div id='menu-line'></div>");

    var foundActive = false, activeElement, linePosition = 0, menuLine = $("#nav #menu-line"), lineWidth, defaultPosition, defaultWidth;

    $("#nav > ul > li").each(function () {
      if ($(this).hasClass('active')) {
        activeElement = $(this);
        foundActive = true;
      }
    });

    if (foundActive === false) {
      activeElement = $("#nav > ul > li").first();
    }

    defaultWidth = lineWidth = activeElement.width();

    defaultPosition = linePosition = activeElement.position().left;

    menuLine.css("width", lineWidth);
    menuLine.css("left", linePosition);

    $("#nav > ul > li").hover(function () {
      activeElement = $(this);
      lineWidth = activeElement.width();
      linePosition = activeElement.position().left;
      menuLine.css("width", lineWidth);
      menuLine.css("left", linePosition);
    },
      function () {
        menuLine.css("left", defaultPosition);
        menuLine.css("width", defaultWidth);
      });
  });
});


zbp.plugin.unbind("comment.reply.start", "system");
zbp.plugin.on("comment.reply.start", "zbp_long", function (id) {
  var i = id;
  $("#inpRevID").val(i);
  var frm = $('#divCommentPost');
  var cancel = $("#cancel-reply");

  if (!frm.hasClass("reply-frm"))
    frm.before($("<div id='temp-frm' style='display:none'>")).addClass("reply-frm");
  $('#AjaxComment' + i).before(frm);

  cancel.show().click(function () {
    var temp = $('#temp-frm');
    $("#inpRevID").val(0);
    if (!temp.length || !frm.length) return;
    temp.before(frm);
    temp.remove();
    $(this).hide();
    frm.removeClass("reply-frm");
    return false;
  })
  try {
    $('#txaArticle').focus();
  } catch (e) {

  }
  return false;
})

zbp.plugin.on("comment.get", "zbp_long", function (logid, page) {
  $('span.commentspage').html("Waiting...");
})

zbp.plugin.on("comment.got", "zbp_long", function () {
  $("#cancel-reply").click();
})

zbp.plugin.on("comment.post.success", "zbp_long", function () {
  $("#cancel-reply").click();
})


